﻿#include "Goomba.h"
//#include "Global.h"
#include "Resource.h"

Goomba::Goomba(float x, float y)
{
	check_turn = false;

	//_sprite = new Sprite(ResourceMng::GetInst()->GetTexture(L"..\\Resources\\Images\\mario.png"), 150, 0);
	_x = x;
	_y = y;
	_vx = (float)GOOMBA_VX;
	_vy = 0;
}

Goomba::Goomba(LPDIRECT3DDEVICE9 d3ddv,  LPDIRECT3DSURFACE9 BackBuffer, LPD3DXSPRITE SpriteHandler, float x, float y, int Object_State, int Direction)
{
	check_turn = false;

	_x = x;
	_y = y;
	
	_vx = -(float)GOOMBA_VX;
	_vy = 0;
	_accelY = 0;
	_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(L"..\\Resources\\Images\\Goomba_32.png"), 100, 0);

	timeDie = 0;

	collisionObjectList = new list<Object*>;

	//object_State = ES_ACTIVING;

	object_State = Object_State;
	direction = Direction;
	if (object_State == ES_ACTIVING)
	{
		if (direction == 1)			//sang trai
		{
			_vx = -(float)GOOMBA_VX;
		} 
		else if (direction == 2)	//sang phai
		{
			_vx = (float)GOOMBA_VX;
		}
	} 
	
}

Goomba::~Goomba()
{
	/*if (_sprite != NULL)
	delete _sprite;*/
	if (collisionObjectList){
		delete collisionObjectList;
		//collisionObjectList = NULL;
	}
}


//void Goomba::Init(LPDIRECT3DDEVICE9 d3ddv,  LPDIRECT3DSURFACE9 BackBuffer, LPD3DXSPRITE SpriteHandler)
//{
//	_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(L"..\\Resources\\Images\\Goomba.png"), 150, 0);
//}

//void Goomba::Update(int _ScreenWidth, int _ScreenHeight, float gameTime)
//{
//	_x += _vx * gameTime;
//	_y += _vy * gameTime;
//
//	ResetRect();
//
//	if(_vx > 0)
//	{
//		TurnRight();
//	}
//	else
//	{
//		TurnLeft();
//	}
//
//	if(_x >= _ScreenWidth)
//	{
//		TurnLeft();
//	}
//	else if(_x <= 0)
//	{
//		TurnRight();
//	}
//
//	if(_vx != 0)
//		//turtle_cur->Update(time);
//		_sprite->Update(gameTime);
//}

void Goomba::Render()
{
	
	if (object_State != ES_REMOVE)
	{
		_sprite->Render((int)_x,(int)_y);
	}
	
}

OBJECT_TYPE Goomba::GetTypeObject()
{
	return OBJECT_ENEMY;
}

/**--------------------------------------------------------
*Description : Turn left Turtle
*Method name : TurnLeft
*Parameters :   
*Return value : void
--------------------------------------------------------**/
void Goomba :: TurnLeft()
{
	check_turn = true;
	//turtle_cur = turtle_left;
	_sprite->SetFrame(1, 9);
	_vx = (float)-GOOMBA_VX;
}


/**--------------------------------------------------------
*Description : Turn right Turtle
*Method name : TurnRight
*Parameters :   
*Return value : void
--------------------------------------------------------**/
void Goomba :: TurnRight()
{
	check_turn = false;
	//turtle_cur = turtle_right;
	_sprite->SetFrame(12, 20);
	_vx = (float)GOOMBA_VX;
}

void Goomba::Update(list<Object*>* staObjs, list<Object*>* dynObjs, DxInput* input, float gameTime)
{
	switch (object_State)
	{
	case ES_ACTIVING:
		Move(staObjs, gameTime);
		_sprite->Update(gameTime);
		if (_y > 600)	//screen height : 600
		{
			ChangeState(ES_REMOVE);
		}
		break;
	case ES_DIED:
		timeDie += gameTime;
		if (timeDie > 900)
		{
			ChangeState(ES_REMOVE);
		}
		_sprite->Update(gameTime);
		ResetRect();
		break;
	case ES_FALL:
		_x += _vx * gameTime;
		_vy += _accelY * gameTime;
		_y += _vy * gameTime + 1/2 * _accelY * gameTime *gameTime;
		if (_y > 600)
		{
			ChangeState(ES_REMOVE);
		}
		//ResetRect();
		ResetRectNULL();
		_sprite->Update(gameTime);
		break;
	}
	
	
}

void Goomba::Move(list<Object*>* staObjs, float gameTime)
{
	list<Object*>::iterator i;
	Object* obj;

	collisionObjectList->clear();
	for (i = staObjs->begin(); i != staObjs->end(); i++)
	{
		obj = *i;
		if (obj->GetTypeObject() == OBJECT_LAND)
		{
			collisionObjectList->push_back(obj);
		}
	}

	//Xet va cham theo truc X
	_x += _vx * gameTime;
	ResetRect();

	for (i = staObjs->begin(); i != staObjs->end(); i++)
	{
		obj = *i;
		if (Collision::RectVsRect(this->GetBond(), obj->GetBond()))
		{
			//xet va cham gach, da, ong,...
			//if (obj->GetID() >= 21 && obj->GetID() <= 24)
			if (obj->GetTypeObject() == OBJECT_LAND)
			{
				_vx = -_vx;
				_x = _xOld;
			}
		}
		ResetRect();
	}
	if(_vx > 0)
	{
		TurnRight();
	}
	else
	{
		TurnLeft();
	}

	//Xet va cham theo truc Y
	_accelY = GRAVITY;
	_vy += _accelY * gameTime;
	_y += _vy * gameTime + 1/2.0 * _accelY * gameTime * gameTime;
	ResetRect();


	//kiem tra va cham nam tren gach, da
	if(collisionObjectList->size() > 0)
	{
		Object* obj;
		list<Object*>::iterator i;
		for(i = collisionObjectList->begin(); i != collisionObjectList->end(); i++)
		{
			obj = *i;
			//kiem tra va cham nam tren gach, da,... khong cho roi xuong
			if(this->GetBond().bottom <= obj->GetBond().bottom && this->GetBond().left < obj->GetBond().right && this->GetBond().right > obj->GetBond().left)
				if(obj->GetY() - this->_sprite->texture->_Height < this->_y)
				{
					_vy = 0;
					_y = obj->GetY() - this->GetSprite()->texture->_Height;
				}
		}
		ResetRect();
	}


	_xOld = _x;
	_yOld = _y;

	if (_vx >= 0)
	{
		direction = 2;
	} 
	else
	{
		direction = 1;
	}
}

void Goomba::ChangeState(int state)
{
	object_State = state;
	switch (object_State)
	{
	case ES_ACTIVING:
		_vx = GOOMBA_VX;
		_accelY = GRAVITY;
		break;
	case ES_FALL:
		if (check_turn == false)
		{
			_sprite->SetFrame(21,21);
		} 
		else
		{
			_sprite->SetFrame(0,0);
		}
		_vy = -1.5;
		_accelY = GRAVITY;
		//_sprite->SetFrame(0,0);
		break;
	case ES_DIED:
		if (check_turn == false)
		{
			_sprite->SetFrame(11,12);
		} 
		else
		{
			_sprite->SetFrame(10,11);
		}
		
		_vx = 0;
		break;
	}
}

//void Goomba::ResetRect()
//{
//	//dang them vao
//	_rectBond.left = _x;
//	_rectBond.right = _rectBond.left + _sprite->texture->_Width;
//	_rectBond.top = _y;
//	_rectBond.bottom = _rectBond.top + _sprite->texture->_Height;
//}

void Goomba::ResetRectNULL()
{
	//dang them vao
	_rectBond.left = 0;
	_rectBond.right = _rectBond.left + 0;
	_rectBond.top = 0;
	_rectBond.bottom = _rectBond.top + 0;
}